package com.amos.muexamclient.sys.service.impl;

import com.amos.exception.BizExceptionEnum;
import com.amos.muexamclient.sys.entity.SysDictTypeEntity;
import com.amos.muexamclient.sys.mapper.SysDictTypeMapper;
import com.amos.muexamclient.sys.service.SysDictTypeService;
import com.amos.muexamclient.sys.service.dto.SysDictTypeDTO;
import com.amos.muexamcore.core.AbstractPageService;
import com.amos.muexamcore.core.BaseMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * Copyright © 2018 五月工作室. All rights reserved.
 *
 * @Package com.amos.muexamclient.sys.service.impl
 * @ClassName SysDictTypeServiceImpl
 * @Description TODO
 * @Author Amos
 * @Modifier
 * @Date 2020-03-24 14:34:19
 * @Version 1.0
 **/
@Service
public class SysDictTypeServiceImpl extends AbstractPageService<SysDictTypeDTO, SysDictTypeEntity> implements SysDictTypeService {

    @Autowired
    private SysDictTypeMapper sysDictTypeMapper;

    /**
     * 条件查询
     *
     * @param dto 查询的条件
     */
    @Override
    public List<SysDictTypeDTO> list(SysDictTypeDTO dto) {
        return this.getMapper().select(dto);
    }

    /**
     * 返回BaseDao对象
     */
    @Override
    public BaseMapper<SysDictTypeEntity, SysDictTypeDTO> getMapper() {
        return sysDictTypeMapper;
    }

    /**
     * 保存之间的校验
     *
     * @param dto 保存的对象
     */
    @Override
    public void preValidate(SysDictTypeDTO dto) {
        this.validateDictType(dto.getDictType(), dto.getId());
    }

    /**
     * 添加子集数据
     *
     * @param id  主表ID
     * @param dto 主表DTO对象
     */
    @Override
    public void appendSubsidiary(String id, SysDictTypeDTO dto) {

    }

    @Override
    public void validateDictType(String dictType, String id) {
        SysDictTypeDTO dto = new SysDictTypeDTO();
        dto.setDictType(dictType);
        List<SysDictTypeDTO> list = this.list(dto);
        if (CollectionUtils.isEmpty(list)) {
            return;
        }
        if (StringUtils.isEmpty(id)) {
            BizExceptionEnum.DEFINED_EXCEPTION.definedMsg("新增字典" + dto.getDictName() + "失败，字典类型已存在").throwException();
        } else {
            if (!list.get(0).getId().equalsIgnoreCase(id)) {
                BizExceptionEnum.DEFINED_EXCEPTION.definedMsg("修改字典" + dto.getDictName() + "失败，字典类型已存在").throwException();
            }
        }
    }

    @Override
    public List<SysDictTypeDTO> optionselect() {
        SysDictTypeDTO dto = new SysDictTypeDTO();
        return this.list(dto);
    }
}
